package servlets;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.impl.DoctorDaoImpl;
import entity.Doctor;
import util.GetInitPwdById;
import util.DateUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:
 * @Date: 2020-05-01
 * @Description: @PACKAGE:${PACKAGE_NAME}
 * @version: 1.0
 */
@WebServlet(name = "医生信息操作",value = "/operateDoctorInfoServlet")
public class OperateDoctorInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String json = "";
        int i = 0;
        DoctorDaoImpl doctorDao = new DoctorDaoImpl();
        String flag = request.getParameter("purpose");
        String id = request.getParameter("Doctor_id");
        String pwd = GetInitPwdById.Get(id);
        String name = request.getParameter("Doctor_name");
        String tel = request.getParameter("Doctor_tel");
        String sex = request.getParameter("Doctor_sex");
        String edu = request.getParameter("Doctor_edu");
        String joindate = request.getParameter("Doctor_joindate");
        String admincheck = "";
        String other = request.getParameter("Doctor_other");
        String picture = request.getParameter("Doctor_picture");
        String post = request.getParameter("Doctor_post");
        String email = request.getParameter("Doctor_email");
        Doctor doctor = new Doctor(id, pwd, name, tel, sex, edu, joindate, admincheck, other, picture, post, email);
        if (flag.equals("0")) {
        	doctor.setJoindate(DateUtil.getStringDateShort());
            i = doctorDao.add(doctor);
        } else if (flag.equals("1")) {
        	doctor.setJoindate(doctorDao.getByDoctorId(id).get(0).getJoindate());
        	doctor.setAdmincheck(doctorDao.getByDoctorId(id).get(0).getAdmincheck());
            i = doctorDao.updateNoPwd(doctor);
        }
        if (i == 1) {
            json = "{\"flag\":\"提交成功\"}";
        } else {
            json = "{\"flag\":\"提交失败，请重试\"}";
        }

        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getWriter(), json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}